package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.LeaveVO;
import com.icss.beans.TravelVO;
import com.icss.daos.LeaveDao;
import com.icss.daos.TravelDao;

public class ApproveServlet extends HttpServlet {

	/**
	 * 请假出差审批
	 */
	public ApproveServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String leave=request.getParameter("leave");
		String travel=request.getParameter("travel");
		String travelId=request.getParameter("travelId");
		String leaveId=request.getParameter("leaveId");
		List<LeaveVO> list=new ArrayList<LeaveVO>();
		LeaveDao dao=new LeaveDao();
		//查询请假记录
		if(leave!=null){
			if(leave.equals("query")){
				System.out.println("程序进入隐藏域了");
				String sql="select * from leave where state="+0;
				System.out.println("SQL语句"+sql);
								
				list=dao.selectLeave(sql);
				if(list!=null){
				request.setAttribute("queryLeave", list);
				String url="/page/attendance/kaoqinShenpi/leave.jsp";
 			    RequestDispatcher rd=request.getRequestDispatcher(url);
 			    rd.forward(request, response);
				}else{System.out.println("LIST为空");}
			}
			if(leave.equals("edit_1")){
				if(leaveId!=null){
					System.out.println("ID为"+leaveId);
					String sql="update leave set state="+1+" where leaveId="+leaveId;
					dao.editLeave(sql);
				}
							
			}
			if(leave.equals("edit_2")){
				if(leaveId!=null){
				String sql="update leave set state="+2+" where leaveId="+leaveId;
				System.out.println("ID为"+leaveId);				
				dao.editLeave(sql);
				}
			}		
		}
		
		//查询出差记录
		if(travel!=null){
			TravelDao dao1=new TravelDao();
			List<TravelVO> list1=new ArrayList<TravelVO>();
			if(travel.equals("query")){
				System.out.println("程序进入隐藏域了");
				String sql="select * from travel where state="+0;
				System.out.println("SQL语句"+sql);
								
				list1=dao1.selectTravel(sql);
				if(list1!=null){
				request.setAttribute("queryTravel", list1);
				String url="/page/attendance/kaoqinShenpi/chuchai.jsp";
 			    RequestDispatcher rd=request.getRequestDispatcher(url);
 			    rd.forward(request, response);
				}else{System.out.println("LIST1为空");}
			}
			if(travel.equals("edit_1")){
				if(travelId!=null){
					System.out.println("ID为"+travelId);
					String sql="update travel set state="+1+" where travelId="+travelId;
					dao1.editTravel(sql);
					String url="/page/attendance/kaoqinShenpi/chuchai.jsp";
	 			    RequestDispatcher rd=request.getRequestDispatcher(url);
	 			    rd.forward(request, response);
				}
							
			}
			if(travel.equals("edit_2")){
				if(travelId!=null){
				String sql="update travel set state="+2+" where travelId="+travelId;
				System.out.println("ID为"+travelId);				
				dao1.editTravel(sql);
				}
			}		
		}	
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
